package com.springapp.dao.dao.impl;

import com.springapp.dao.dao.IEmplacementDAO;
import com.springapp.model.Emplacement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by nicol_000 on 27/08/14.
 */
@Repository
public class EmplacementDAO implements IEmplacementDAO {

    @Qualifier("dataSource") //Fait référence au datasource dans le mvc-dispatcher.xml
    @Autowired
    private DataSource dataSource;

    public Emplacement getEmplacement(int _idEmplacement){
        Emplacement emp = new Emplacement();
        Boolean bReq = true;
        String sql="" +
                "SELECT *\n" +
                "FROM Emplacement Emp\n" +
                "WHERE Emp.IDEmplacement='"+_idEmplacement+"'\n";

        Connection conn = null;
        try {
            conn=dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet result=ps.executeQuery();
            if(result.next()){
                emp.setIdEmplacement(result.getInt("IDEmplacement"));
                emp.setsDescr(result.getString("descr"));
                emp.setiAllee(result.getInt("allee"));
                emp.setiRang(result.getInt("rang"));
                emp.setiQteMaxZS(result.getInt("QteMaxZoneStock"));
                emp.setiQteActuelleZS(result.getInt("QteActuelleZoneStock"));
            }else{bReq=false;}
            result.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(bReq){
            return emp;
        }else{
            return null;
        }
    }

    public String update(Emplacement emp){
        String sRet = "";
        String sql="" +
                "UPDATE Emplacement SET descr='"+emp.getsDescr()+"'\n," +
                "allee='"+emp.getiAllee()+"'\n," +
                "rang='"+emp.getiRang()+"'\n," +
                "qTeMaxZoneStock='"+emp.getiQteMaxZS()+"'\n," +
                "qteActuelleZoneStock='"+emp.getiQteActuelleZS()+"'\n," +
                "EstPicking='"+emp.isbEstPicking()+"'\n" +
                "WHERE IDEmplacement='"+emp.getIdEmplacement()+"'";
        System.out.println(sql);
        Connection conn = null;
        try {
            conn=dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            int iRet=ps.executeUpdate();
            ps.close();
            if(iRet==0){
                sRet="Echec de la modification de l'emplacement";
            }else{
                sRet="Emplacement mis à jour";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sRet;
    }
}
